package com.imooc.service.impl;

import com.imooc.common.constants.ResultCodeConstants;
import com.imooc.common.enums.Sex;
import com.imooc.common.result.ResponseData;
import com.imooc.common.result.ResponseUtils;
import com.imooc.common.utils.DateUtils;
import com.imooc.common.utils.ExceptionProcessorUtils;
import com.imooc.common.utils.MD5Utils;
import com.imooc.dto.user.UserLoginRequest;
import com.imooc.dto.user.UserLoginResponse;
import com.imooc.dto.user.UserRegistRequest;
import com.imooc.dto.user.UserRegistResponse;
import com.imooc.mapper.StuMapper;
import com.imooc.mapper.UsersMapper;
import com.imooc.pojo.Stu;
import com.imooc.pojo.Users;
import com.imooc.pojo.bo.UserBO;
import com.imooc.service.IStuService;
import com.imooc.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: StuServiceImpl
 * @description:
 * @author: limx
 * @create: 2019-12-21 08:43
 **/
@Slf4j
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UsersMapper usersMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResponseData queryUsernameIsExist(String username) {
        log.info("Begin UserServiceImpl.queryUsernameIsExist username: [{}]",username);
        if (StringUtils.isBlank(username)) {
            return ResponseUtils.errorMsg("用户名不能为空");
        }
        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username",username);
        Users user = usersMapper.selectOneByExample(example);
        if(null == user){
            return ResponseUtils.success();
        }
        return ResponseUtils.errorMsg("用户名已经存在");
    }


    private static final String USER_FACE = "http://122.152.205.72:88/group1/M00/00/05/CpoxxFw_8_qAIlFXAAAcIhVPdSg994.png";

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public UserRegistResponse createUser(UserRegistRequest registRequest) {
        UserRegistResponse response = new UserRegistResponse();
        registRequest.requestCheck();
        log.info("Begin UserServiceImpl.createUser registRequest: [{}]",registRequest);

        Users user = new Users();
        user.setId(new Sid().nextShort());
        user.setUsername(registRequest.getUsername());
        try {
            user.setPassword(MD5Utils.getMD5Str(registRequest.getPassword()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 默认用户昵称同用户名
        user.setNickname(registRequest.getUsername());
        // 默认头像
        user.setFace(USER_FACE);
        // 默认生日
        user.setBirthday(DateUtils.stringToDate("1900-01-01"));
        // 默认性别为 保密
        user.setSex(Sex.secret.type);
        user.setCreatedTime(new Date());
        user.setUpdatedTime(new Date());

        Integer result = usersMapper.insert(user);
        log.info("UserServiceImpl.createUser effect result : [{}]", result);
        response.setNickname(registRequest.getUsername());
        response.setUsername(registRequest.getUsername());
        response.setCode(ResultCodeConstants.SUCCESS.getCode());
        response.setMsg(ResultCodeConstants.SUCCESS.getMessage());
        return response;
    }

    @Override
    public UserLoginResponse userLogin(UserLoginRequest request) {
        request.requestCheck();
        log.info("Begin UserServiceImpl.userLogin request: [{}]",request);
        UserLoginResponse response = new UserLoginResponse();
        Example example = new Example(Users.class);
        try {
            Example.Criteria userCriteria = example.createCriteria();
            userCriteria.andEqualTo("username", request.getUsername());
            userCriteria.andEqualTo("password", MD5Utils.getMD5Str(request.getPassword()));
            Users result = usersMapper.selectOneByExample(example);
            if (result == null) {
                response.setCode(ResultCodeConstants.USER_LOGIN_ERROR.getCode());
                response.setMsg(ResultCodeConstants.USER_LOGIN_ERROR.getMessage());
            } else {
                response.setUsername(result.getUsername());
                response.setNickname(result.getNickname());
                response.setCode(ResultCodeConstants.SUCCESS.getCode());
                response.setMsg(ResultCodeConstants.SUCCESS.getMessage());
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("UserServiceImpl.userLogin Occur Exception : ",e);
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }
}
